import 'package:core_package/core_package.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_unionad/model/app_device_list_model.dart';
import 'package:flutter_app_unionad/model/channel_model.dart';
import 'package:flutter_app_unionad/model/devece_type_model.dart';
import 'package:flutter_app_unionad/model/work_take_model.dart';
import 'package:flutter_app_unionad/request/base_request.dart';

class WorkBenchRequest {
  //获取任务列表
  static Future<List<WorkTakeModel>?> bookingList({
    int pageNum = 0,
    int pageSize = 20,
    int timeSort = 0, //时间排序： 0降序，1升序
    String? userCityCode,
    String? userCountyCode,
    String? userName,
    String? userProvinceCode,
  }) async {
    Map<String, dynamic> map = {'pageNum': pageNum, 'pageSize': pageSize, "timeSort": timeSort};
    if (userCityCode != null && userCityCode.stringIsNotEmpty) map['userCityCode'] = userCityCode;
    if (userCountyCode != null && userCountyCode.stringIsNotEmpty) map['userCountyCode'] = userCountyCode;
    if (userName != null && userName.stringIsNotEmpty) map['userName'] = userName;
    if (userProvinceCode != null && userProvinceCode.stringIsNotEmpty) map['userProvinceCode'] = userProvinceCode;

    final future = await HttpRequest.postRequest("/api/booking/bookingList", params: map);
    List<WorkTakeModel>? list;
    baseRequestAnalysisData(future, (map) {
      //
      list = [];
      for (var v in map['list']) {
        list?.add(WorkTakeModel.fromJson(v));
      }
    }, (info, code) {
      //
    });
    return list;
  }

  //取消任务
  static Future<bool> cancleBooking({String? id, String? remark}) async {
    print("object");
    final future = await HttpRequest.postRequest("/api/booking/cancelBooking", params: {"id": id, "remark": remark});
    bool result = false;
    baseRequestAnalysisData(future, (map) {
      ShowToastWidget.showToast(msg: '取消成功');
      result = true;
    }, (info, code) {
      //
      ShowToastWidget.showToast(msg: info);
    });
    return result;
  }

  //激活设备
  static Future<bool> initDev({
    @required String? channelId,
    @required String? channelType,
    @required String? contractName,
    @required String? contractTel,
    @required String? createBy,
    @required String? devAddress,
    @required String? devCity,
    @required String? devCityCode,
    @required String? devCounty,
    @required String? devCountyCode,
    @required String? devProvince,
    @required String? devProvinceCode,
    @required String? devSn,
    @required String? id,
    @required String? operType,
    @required String? userName,
    @required String? userType,
    @required String? bookingId,
    @required String? modelId,
  }) async {
    Map<String, dynamic> map = {
      'channelId': channelId,
      'channelType': channelType,
      'contractName': contractName,
      'contractTel': contractTel,
      'createBy': createBy,
      'devAddress': devAddress,
      'devCity': devCity,
      'devCityCode': devCityCode,
      'devCounty': devCounty,
      'devCountyCode': devCountyCode,
      'devProvince': devProvince,
      'devProvinceCode': devProvinceCode,
      'devSn': devSn,
      'id': id,
      'operType': operType,
      'userName': userName,
      'userType': userType,
      'bookingId': bookingId,
      'modelId': modelId,
    };
    bool result = false;
    final future = await HttpRequest.postRequest('/api/device/initDev', params: map);
    baseRequestAnalysisData(future, (data) {
      //
      result = true;
    }, (info, code) {
      ShowToastWidget.showToast(msg: info);
    });
    return result;
  }

  static Future<List<ChannelModel>?> getChannelList() async {
    final future = await HttpRequest.postRequest('/api/channel/listChannels');
    List<ChannelModel>? list;

    baseRequestAnalysisData(future, (map) {
      list = [];

      for (var m in map) {
        list?.add(ChannelModel.fromJson(m));
      }
    }, (info, code) {});
    return list;
  }

  static Future<List?> getDeviceNumberList() async {
    final future = await HttpRequest.postRequest('/api/device/listDevSn');
    List? list;

    baseRequestAnalysisData(future, (map) {
      list = map;
    }, (info, code) {});
    return list;
  }

  static Future<List<DeviceTypeModel>?> getDeviceModelList() async {
    final future = await HttpRequest.postRequest('/api/model/getModelList');
    List<DeviceTypeModel>? list;

    baseRequestAnalysisData(future, (map) {
      list = [];
      print("object");

      for (var m in map) {
        list?.add(DeviceTypeModel.fromJson(m));
      }
    }, (info, code) {});
    return list;
  }

  //获取app设备
  static Future<List<AppDeviceListModel>?> getAppDeviceList({
    int pageNum = 0,
    int pageSize = 20,
    String? keyword,
  }) async {
    Map<String, dynamic> map = {'pageNum': pageNum, 'pageSize': pageSize};

    if (keyword != null && keyword.stringIsNotEmpty) map['keyword'] = keyword;

    final future = await HttpRequest.postRequest("/api/device/appDeviceList", params: map);
    List<AppDeviceListModel>? list;
    baseRequestAnalysisData(future, (map) {
      //
      list = [];
      for (var v in map['data']) {
        list?.add(AppDeviceListModel.fromJson(v));
      }
    }, (info, code) {
      //
    });
    return list;
  }

  //设备列表 激活设备
  static Future<bool> appInitDeviceOnly({String? devSn}) async {
    final future = await HttpRequest.getRequest('/api/device/appInitDeviceOnly', params: {'devSn': devSn});
    bool result = false;
    baseRequestAnalysisData(future, (data) {
      result = true;
      ShowToastWidget.showToast(msg: '激活成功');
    }, (info, code) {
      ShowToastWidget.showToast(msg: info);
    });
    return result;
  }

  //设备列表 激活设备
  static Future<bool> appPayCalibration({String? devSn}) async {
    final future = await HttpRequest.getRequest('/api/device/appPayCalibration', params: {'devSn': devSn});
    bool result = false;
    baseRequestAnalysisData(future, (data) {
      result = true;
      ShowToastWidget.showToast(msg: '启动成功');
    }, (info, code) {
      ShowToastWidget.showToast(msg: info);
    });
    return result;
  }
}
